﻿using log4net;
using Quartz;
using Quartz.Listener;
using System.Threading;
using System.Threading.Tasks;

namespace QuartzTest
{
    public class LogJobListener : JobListenerSupport
    {
        static readonly ILog log = LogManager.GetLogger(typeof(LogJobListener));
        public override string Name => "LogJobListener";
        
        public override Task JobExecutionVetoed(IJobExecutionContext context, CancellationToken cancellationToken = default)
        {
            log.Debug($"JobExecutionVetoed JobDetail:{context.JobDetail.JobType}");
            
            return Task.CompletedTask;
        }        
        public override Task JobToBeExecuted(IJobExecutionContext context, CancellationToken cancellationToken = default)
        {
            log.Debug($"JobToBeExecuted JobDetail:{context.JobDetail.JobType}");
            return Task.CompletedTask;
        } 
        
        public override Task JobWasExecuted(IJobExecutionContext context, JobExecutionException jobException, CancellationToken cancellationToken = default)
        {
            if (jobException != null)
            {
                EE.LogError("JobWasExecuted:", jobException.InnerException, log);
            }
            log.Debug($"JobWasExecuted JobDetail:{context.JobDetail.JobType},jobException={jobException}");
            return Task.CompletedTask;
        }
    }
}
